import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions,URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import * as Mock from 'mockjs';

import { formData } from '../../model/post-model';

@Injectable()
export class PostlistService {
  public mainDataMockUrl='mock-data/main-data-mock.json'
  public mydata;
  constructor(public http:Http) {

                                                                                // 手动造的假数据
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    var name1=['在家就能做','卖牛奶','卖猪肉','养猪','养羊','养马','养猴儿']
    var img1=['<img src=\'http://www.jianzhimao.com/web/assets/img/hot.png\'/>','','']
    var obj={
      'host':'www.baidu',
      'port':'12345',
      'node':'selector'
    }
    const data = Mock.mock({
      'items|453': [{
        'id|+1': 1,
        'address':'@city(true)',
        'date':'@date("yyyy-MM-dd")',
        'name|1':name1,
        'img|1':img1,
        'num|1-100000':0
      }],'total':453
    });
    this.mydata=data;


//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


  }
  
  public getPostList(searchText: string,page:number=1):Observable<formData[]>{
    let url = this.mainDataMockUrl;
    let params = new URLSearchParams();
    if (searchText) {
			params.set('searchText',searchText);  //post设置参数
      url = this.mainDataMockUrl;
      console.log(`searchText=${searchText}`);
		}
    params.set('page',String(page));


    return this.http
               .get(url,{search:params})
               .map((res:Response) => {
                   let result=res.json();
                   console.log("获得假数据---->",this.mydata);
                   return this.mydata;
               })
               .catch((error:any) => Observable.throw(error || 'Server error'));//抛出异常
  }

  public getPostNumber():number{
    return 0;
  }

  public addPost(user:any){

  }

  public search() {
    
  }
}
